#ifndef UNITITEMMODEL_H
#define UNITITEMMODEL_H

#include <QAbstractItemModel>
#include <QVector>

#include "other/yalb_abstracttreemodel.h"
#include "collections/dualcollection.h"
#include "other/gameStructs.h"

class RosterUnit;

class UnitItemModelItem : public yALB_AbstractTreeItem {
    friend class UnitItemModel;
public:

    UnitItemModelItem(const QString& _key, float _cost, int _number, yALB_AbstractTreeItem *parent);

    const QString key;
    float cost;
    int number;

    UnitItemModelItem* parent() const;
    UnitItemModelItem* child(int i) const;
};

class UnitItemModel : public yALB_AbstractTreeModel
{
    Q_OBJECT
public:
    UnitItemModel(const DualCollection<GameItem> *items, const DualCollection<GameCategory> *categories, QObject *parent = 0);
    ~UnitItemModel();

    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    void setSource(const RosterUnit *unit);
    void buildCategoryIndex();

    void updateDataForKey(const QString& key);
    QString getKeyFromIndex(const QModelIndex& index) const;
    
private:
    const DualCollection<GameItem> *_items;
    const DualCollection<GameCategory> *_categories;

    const RosterUnit *currentUnit;

    QHash<QString, QStringList> categoryItems; // Item list (keys) for each category (key)
    QHash<QString, UnitItemModelItem*> categoryIndex; // Category index
    QHash<QString, UnitItemModelItem*> itemIndex;

    void addItemRow(const QString& key, uint number);

    enum { NameColumn = 0, NumberColumn, CostColumn, ColumnNumber };
};
#endif // UNITITEMMODEL_H
